Gapminder
Graphs with gapminder data
Data
# devtools::install_github("derekmichaelwright/agData")
library(agData)
library(gapminder)
library(gganimate)
myCaption <- "www.dblogr.com/ or derekmichaelwright.github.io/dblogr/ | Data: gapminder"
gapminder %>% arrange(year) %>% as_tibble()## # A tibble: 1,704 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Albania Europe 1952 55.2 1282697 1601.
## 3 Algeria Africa 1952 43.1 9279525 2449.
## 4 Angola Africa 1952 30.0 4232095 3521.
## 5 Argentina Americas 1952 62.5 17876956 5911.
## 6 Australia Oceania 1952 69.1 8691212 10040.
## 7 Austria Europe 1952 66.8 6927772 6137.
## 8 Bahrain Asia 1952 50.9 120447 9867.
## 9 Bangladesh Asia 1952 37.5 46886859 684.
## 10 Belgium Europe 1952 68 8730405 8343.
## # ℹ 1,694 more rows
Plots
By Year
mp <- ggplot(gapminder %>% filter(year %in% c(1952, 1982, 2007)),
aes(x = gdpPercap / 1000, y = lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(. ~ year, ncol = 5) +
theme_agData(legend.position = "none") +
labs(x = "GDP Per Capita ($1000)", y = "Life Expectancy", caption = myCaption)
ggsave("gapminder_01.png", mp, width = 6, height = 4)Life Expectancy By Region
mp <- ggplot(gapminder, aes(x = year, y = lifeExp, colour = country)) +
geom_line(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_x_continuous(breaks = seq(1950, 2010, 20)) +
coord_cartesian(xlim = c(1950, 2010)) +
facet_wrap(. ~ continent, ncol = 5) +
theme_agData(legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = NULL, y = "Life Expectancy", caption = myCaption)
ggsave("gapminder_02.png", mp, width = 10, height = 5)GDP Per Capita By Region
mp <- ggplot(gapminder, aes(x = year, y = gdpPercap / 1000, colour = country)) +
geom_line(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_x_continuous(breaks = seq(1950, 2010, 20)) +
scale_y_log10() +
coord_cartesian(xlim = c(1950, 2010)) +
facet_wrap(. ~ continent, ncol = 5) +
theme_agData(legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = NULL, y = "GDP Per Capita ($1000)", caption = myCaption)
ggsave("gapminder_03.png", mp, width = 10, height = 5)Animations
GDP Per Capita x Life Expectancy
# gganimate example
mp <- ggplot(gapminder, aes(x = gdpPercap / 1000, y = lifeExp,
size = pop, fill = country)) +
geom_point(alpha = 0.7, show.legend = F, pch = 21, color = "black") +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(. ~ continent, ncol = 5) +
theme_agData() +
labs(title = 'Year: {frame_time}', caption = myCaption,
y = 'Life Expectancy', x = 'GDP Per Capita ($1000)') +
transition_time(year)
anim_save("gapminder_gif_01.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)GDP Per Capita By Region
# Prep data
xx <- gapminder %>% arrange(rev(year), gdpPercap) %>%
mutate(country = factor(country, levels = unique(country)))
# Plot
mp <- ggplot(xx, aes(x = country, y = gdpPercap / 1000,
size = pop, fill = country)) +
geom_point(alpha = 0.7, show.legend = F, pch = 21, color = "black") +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_y_log10() +
theme_agData(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(title = 'Year: {frame_time}', caption = myCaption,
x = NULL, y = 'GDP Per Capita ($1000)') +
transition_time(year)
anim_save("gapminder_gif_02.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Life Expectancy By Region
# Prep data
xx <- gapminder %>% arrange(rev(year), lifeExp) %>%
mutate(country = factor(country, levels = unique(country)))
# Plot
mp <- ggplot(xx, aes(x = country, y = lifeExp,
size = pop, fill = country)) +
geom_point(alpha = 0.7, show.legend = F, pch = 21, color ="black") +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_y_log10() +
theme_agData(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(title = 'Year: {frame_time}', caption = myCaption,
x = NULL, y = 'Life Expectancy') +
transition_time(year)
anim_save("gapminder_gif_03.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)gg_gapminder1 <- function(x) {
ggplot(x, aes(x = gdpPercap / 1000, y = lifeExp,
size = pop, fill = country)) +
geom_point(alpha = 0.7, pch = 21, color = "black") +
geom_text(aes(label = countryLabel), alpha = 0.7, alpha = 0.3) +
scale_fill_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
theme_agData(legend.position = "none") +
labs(title = "Year: {frame_time}", caption = myCaption,
y = "Life Expectancy", x = "GDP Per Capita ($1000)") +
transition_time(year)
}# Prep data
areas <- c("China", "United States", "India")
xx <- gapminder %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
mp <- gg_gapminder1(xx)
anim_save("gapminder_gif_04.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Regions
Africa
# Prep data
areas <- c("South Africa", "Egypt", "Nigeria", "Ethiopia")
xx <- gapminder %>%
filter(continent == "Africa") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
mp <- gg_gapminder1(xx)
anim_save("gapminder_gif_05.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Americas
# Prep data
areas <- c("United States", "Canada", "Brazil", "Mexico")
xx <- gapminder %>%
filter(continent == "Americas") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
mp <- gg_gapminder1(xx)
anim_save("gapminder_gif_06.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Asia
# Prep data
areas <- c("China", "India", "Japan", "Bangladesh")
xx <- gapminder %>%
filter(continent == "Asia") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
mp <- gg_gapminder1(xx)
anim_save("gapminder_gif_07.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Europe
# Prep data
areas <- c("Germany", "France", "Russia", "Turkey")
xx <- gapminder %>%
filter(continent == "Europe") %>%
mutate(countryLabel = ifelse(country %in% areas, as.character(country), NA))
# Plot
mp <- gg_gapminder1(xx)
anim_save("gapminder_gif_08.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Countries
gg_gapminder2 <- function(mycountries) {
x <- gapminder %>% filter(country %in% mycountries)
ggplot(x, aes(x = gdpPercap / 1000, y = lifeExp, fill = country)) +
geom_point(size = 3, pch = 21, color = "black") +
geom_line(alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_x_log10() +
theme_agData(legend.position = "bottom") +
labs(title = "Year: {frame_along}", caption = myCaption,
y = "Life Expectancy", x = "GDP Per Capita ($1000)") +
transition_reveal(year)
}Canada
mp <- gg_gapminder2("Canada")
anim_save("gapminder_gif_09.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)China
mp <- gg_gapminder2("China")
anim_save("gapminder_gif_10.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Rwanda, South Africa, Nigeria
mp <- gg_gapminder2(c("Rwanda", "South Africa", "Nigeria"))
anim_save("gapminder_gif_11.gif", mp,
nframes = 300, fps = 20, end_pause = 60,
width = 900, height = 600, res = 150)Heatmaps
# Prep data
myColors <- c("darkred", "darkgoldenrod2", "steelblue")
xx <- gapminder %>% filter(continent == "Africa")
myYears <- unique(xx$year)
myAreas <- xx %>% filter(year == 2007) %>%
arrange(lifeExp) %>% pull(country) %>% unique()
xx <- xx %>% mutate(country = factor(country, level = myAreas))
# Plot
mp <- ggplot(xx, aes(x = year, y = country, fill = lifeExp)) +
geom_tile(color = "white", size = 0.35) +
scale_x_continuous(expand = c(0,0), breaks = myYears) +
scale_fill_gradientn(name = "Life Expectancy", na.value = 'white',
colors = myColors) +
theme_agData(legend.position = "bottom",
bgFill = "white") +
labs(x = NULL, y = NULL, caption = myCaption)
ggsave("gapminder_04.png", mp, width = 6, height = 10)